package vcp.controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JOptionPane;

import db.parking.beans.Orders;
import db.parking.beans.Subscriber;

import vcp.common.AbstractController;
import vcp.common.IController;
import vcp.gui.forms.CheckInRegularForm;
import vcp.gui.forms.CheckInSubscriberForm;
import vcp.models.CheckInModel;
import vcp.views.CheckInView;

/**
 * Customer check in controller 
 * @author G15
 * 
 *
 */

public class CheckInController extends AbstractController implements
ActionListener, IController {
	CheckInModel checkinModel;
	CheckInView checkinView;

	/* (non-Javadoc)
	 * @see vcp.common.IController#addModel(java.lang.Object)
	 */
	@Override
	public void addModel(Object model) {
		checkinModel = (CheckInModel)model;
		
	}

	/* (non-Javadoc)
	 * @see vcp.common.IController#addView(java.lang.Object)
	 */
	@Override
	public void addView(Object view) {
		checkinView = (CheckInView)view;
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if(command.equals("CheckInRegular")){
			CheckInReg();
		}else if(command.equals("CheckInSubscribed")){
			CheckInSubscriber();
		}
	}
	
		/**
		 *  Check in regular
		 */
		private void CheckInReg() {
			boolean isValidForm=false;
			Orders bean = new Orders();
			
			CheckInRegularForm form = checkinView.getForm().getCheckinRegularForm();
			
	        if(form.getTxtCheckIn().getText().isEmpty() ||form.getOrderNo_textField().getText().isEmpty() )
	        {
	        	JOptionPane.showMessageDialog(null,"Please fill in all fields","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
			    isValidForm = false;
			    return;
	        }
	        
	        //Parse all data	
			String cusID = form.getTxtCheckIn().getText();
			int oid = Integer.parseInt(form.getOrderNo_textField().getText());
			
			
			//Input Validity Check
			if (cusID.equals("") || Integer.parseInt(cusID)<0 ||oid< 0 ) {
				JOptionPane.showMessageDialog(null,"Invalid ID Entered,Please Re-Enter","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
				isValidForm = false;
			}

		   else{
				isValidForm = true;
			}
			
			if(isValidForm==true){
				 try {
					//create new Timestamp for the time the user wants to leave the lot
					bean.setUid(cusID);
					bean.setOid(oid);
					checkinModel.checkinRegular(bean);
					
					//start the Order Controller to insert order into database	
					} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		

			}
				
			
		}
		
		
		private void CheckInSubscriber() {
			boolean isValidForm=false;
			Subscriber bean = new Subscriber();
			
			CheckInSubscriberForm form = checkinView.getForm().getCheckInSubscriberForm();
			
	        if(form.getSubIdField().getText().isEmpty() ||form.getSubCarNumField().getText().isEmpty() )
	        {
	        	JOptionPane.showMessageDialog(null,"Please fill in all fields","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
			    isValidForm = false;
			    return;
	        }
	        
	        //Parse all data	
			int subID = Integer.parseInt(form.getSubIdField().getText());
			String carNo = form.getSubCarNumField().getText();
			
			
			//Input Validity Check
			if (carNo.equals("") || subID < 0 ||Integer.parseInt(carNo)<0) {
				JOptionPane.showMessageDialog(null,"Invalid ID Entered,Please Re-Enter","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
				isValidForm = false;
			}

		   else{
				isValidForm = true;
			}
			
			if(isValidForm==true){
				 try {
					bean.setSubID(subID);
					bean.setCarNo(carNo);
					checkinModel.checkinSubscribed(bean);					
					} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		

			}
				
			
		}
		
		
	}

